package cn.iocoder.yudao.module.shop.controller.admin.crosstenant;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.shop.controller.admin.crosstenant.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.crosstenant.ShopCrossTenantDO;
import cn.iocoder.yudao.module.shop.convert.crosstenant.ShopCrossTenantConvert;
import cn.iocoder.yudao.module.shop.service.crosstenant.ShopCrossTenantService;

@Tag(name = "管理后台 - 跨店铺")
@RestController
@RequestMapping("/shop/cross-tenant")
@Validated
public class ShopCrossTenantController {

    @Resource
    private ShopCrossTenantService crossTenantService;

    @PostMapping("/create")
    @Operation(summary = "创建跨店铺")
    @PreAuthorize("@ss.hasPermission('shop:cross-tenant:create')")
    public CommonResult<Long> createCrossTenant(@Valid @RequestBody ShopCrossTenantCreateReqVO createReqVO) {
        return success(crossTenantService.createCrossTenant(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新跨店铺")
    @PreAuthorize("@ss.hasPermission('shop:cross-tenant:update')")
    public CommonResult<Boolean> updateCrossTenant(@Valid @RequestBody ShopCrossTenantUpdateReqVO updateReqVO) {
        crossTenantService.updateCrossTenant(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除跨店铺")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('shop:cross-tenant:delete')")
    public CommonResult<Boolean> deleteCrossTenant(@RequestParam("id") Long id) {
        crossTenantService.deleteCrossTenant(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得跨店铺")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:cross-tenant:query')")
    public CommonResult<ShopCrossTenantRespVO> getCrossTenant(@RequestParam("id") Long id) {
        ShopCrossTenantDO crossTenant = crossTenantService.getCrossTenant(id);
        return success(ShopCrossTenantConvert.INSTANCE.convert(crossTenant));
    }

    @GetMapping("/get-by-tenantid")
    @Operation(summary = "获得跨店铺")
    @Parameter(name = "tenantid", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:cross-tenant:query')")
    public CommonResult<List<ShopCrossTenantRespVO>> getCrossTenantList(@RequestParam("tenantid") Long tenantId){
        List<ShopCrossTenantDO> list = crossTenantService.getCrossTenantByTenantId(tenantId);
        return success(ShopCrossTenantConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/list")
    @Operation(summary = "获得跨店铺列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('shop:cross-tenant:query')")
    public CommonResult<List<ShopCrossTenantRespVO>> getCrossTenantList(@RequestParam("ids") Collection<Long> ids) {
        List<ShopCrossTenantDO> list = crossTenantService.getCrossTenantList(ids);
        return success(ShopCrossTenantConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得跨店铺分页")
    @PreAuthorize("@ss.hasPermission('shop:cross-tenant:query')")
    public CommonResult<PageResult<ShopCrossTenantRespVO>> getCrossTenantPage(@Valid ShopCrossTenantPageReqVO pageVO) {
        PageResult<ShopCrossTenantDO> pageResult = crossTenantService.getCrossTenantPage(pageVO);
        return success(ShopCrossTenantConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出跨店铺 Excel")
    @PreAuthorize("@ss.hasPermission('shop:cross-tenant:export')")
    @OperateLog(type = EXPORT)
    public void exportCrossTenantExcel(@Valid ShopCrossTenantExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ShopCrossTenantDO> list = crossTenantService.getCrossTenantList(exportReqVO);
        // 导出 Excel
        List<ShopCrossTenantExcelVO> datas = ShopCrossTenantConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "跨店铺.xls", "数据", ShopCrossTenantExcelVO.class, datas);
    }

}
